package com.ruoyi.system.service.impl;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.DzDeptsync;
import com.ruoyi.system.domain.DzUsersync;
import com.ruoyi.system.domain.vo.AuthUser;
import com.ruoyi.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-01
 */
@Service
public class DzHuiyuanServiceImpl implements IDzHuiyuanService 
{
    @Autowired
    private DzHuiyuanMapper dzHuiyuanMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询用户信息
     * 
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public DzHuiyuan selectDzHuiyuanById(Long id)
    {
        return dzHuiyuanMapper.selectDzHuiyuanById(id);
    }

    @Override
    public DzHuiyuan selectDzHuiyuanByDn(String dn) {

        return dzHuiyuanMapper.selectDzHuiyuanByDn(dn);
    }

    /**
     * 查询用户信息列表
     * 
     * @param dzHuiyuan 用户信息
     * @return 用户信息
     */
    @Override
    public List<DzHuiyuan> selectDzHuiyuanList(DzHuiyuan dzHuiyuan)
    {
        if(dzHuiyuan.getPassword()==null){
            //权限控制
            SysUser user=  SecurityUtils.getLoginUser().getUser();
            List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
            int datascope = 5;
            for(SysRole role:listRole){
                if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                    datascope=Integer.parseInt(role.getDataScope());
                }
            }
            dzHuiyuan.setDatascope(datascope);
            if(datascope==4){
                dzHuiyuan.setCheckDeptId(user.getDeptId());
            }
            if(datascope==5){
                DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
                if(huiyuan!=null){
                    dzHuiyuan.setCheckHuiyuanId(huiyuan.getId());
                }else{
                    dzHuiyuan.setCheckHuiyuanId(0l);
                }

            }
            return dzHuiyuanMapper.selectDzHuiyuanList(dzHuiyuan);
        }else{
            return dzHuiyuanMapper.selectDzHuiyuanList(dzHuiyuan);
        }


    }

    @Override
    public List<DzHuiyuan> selectDzHuiyuanWebList(DzHuiyuan dzHuiyuan) {
        return dzHuiyuanMapper.selectDzHuiyuanList(dzHuiyuan);
    }

    /**
     * 新增用户信息
     * 
     * @param dzHuiyuan 用户信息
     * @return 结果
     */
    @Override
    public int insertDzHuiyuan(DzHuiyuan dzHuiyuan)
    {
        dzHuiyuan.setCreateTime(DateUtils.getNowDate());
        return dzHuiyuanMapper.insertDzHuiyuan(dzHuiyuan);
    }

    /**
     * 修改用户信息
     * 
     * @param dzHuiyuan 用户信息
     * @return 结果
     */
    @Override
    public int updateDzHuiyuan(DzHuiyuan dzHuiyuan)
    {
        return dzHuiyuanMapper.updateDzHuiyuan(dzHuiyuan);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteDzHuiyuanByIds(Long[] ids)
    {
        return dzHuiyuanMapper.deleteDzHuiyuanByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteDzHuiyuanById(Long id)
    {
        return dzHuiyuanMapper.deleteDzHuiyuanById(id);
    }



    @Override
    public String getAccessToken() {
        String url = Constants.platBaseUrl+"/authorize/apiLic/getAccessToken?customKey="+Constants.customKey+"&customSecret="+Constants.customSecret;
        String accessToken = null;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.parseObject(message);
            accessToken = demoJson.getJSONObject("bizData").getString("accessToken");
            System.out.println("获取token："+accessToken);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return accessToken;
    }

    @Override
    public AuthUser getAuthUser(String token,String tmp_auth_code) {
        String url = Constants.platBaseUrl+"/authorize/tla/getuserinfo_bycode/"+tmp_auth_code+"?accessToken="+token;
        AuthUser list = null;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.parseObject(message);
            String jsonObjString =demoJson.getString("bizData");
            list=JSONObject.parseObject(jsonObjString,AuthUser.class);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public DzUsersync getUserInfo(String token, String userCode) {
        String url = Constants.platBaseUrl+"organizeuser/absync/getUserDetail/"+userCode+"?accessToken="+token;
        DzUsersync list = null;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.parseObject(message);
            String jsonObjString =demoJson.getString("bizData");
            list=JSONObject.parseObject(jsonObjString,DzUsersync.class);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }





    @Override
    public DzDeptsync getZuzhi(String token,String unitCode) {
        String url = Constants.platBaseUrl +"organizeuser/absync/getUnitDetail/"+unitCode+"?accessToken="+token;
        DzDeptsync list = null;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson =JSON.parseObject(message);
//            JSONObject demoJson = JSONObject.parseObject(message);
//            String jsonObjString = demoJson.getJSONObject("bizData").getString("unitList");
            String jsonObjString =demoJson.getString("bizData");
            list=JSONObject.parseObject(jsonObjString,DzDeptsync.class);
            System.out.println("获取组织列表："+jsonObjString);
//            list = (List<DzDeptsync>) JSON.parseArray(jsonObjString,DzDeptsync.class);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
